package com.lm.activiti.boot.tools.zip;

import com.beust.jcommander.internal.Lists;
import lombok.SneakyThrows;
import org.apache.commons.lang3.StringUtils;
import org.junit.Test;

import java.io.*;
import java.nio.charset.Charset;
import java.util.UUID;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

/**
 * @author lm_jcf
 * @date 2022/4/12 14:07
 */
public class ZipCase {


    @SneakyThrows
    @Test
    public void unzipWrite(){
        String filePath = "D:\\南宁二中标准数据模型增量\\测试压缩.zip";
        String tmpFile = "D:\\南宁二中标准数据模型增量\\解压后的文件";

        File newFile = null;
        byte[] buffer = new byte[1024];
        ZipInputStream zis = new ZipInputStream(new FileInputStream(filePath), Charset.forName("GBK"));
        ZipEntry zipEntry = null;
        while ((zipEntry = zis.getNextEntry()) != null) {

            String fil = tmpFile + File.separator + StringUtils.join(Lists.newArrayList(zipEntry.getName().split("/")), File.separator);

            newFile = new File(fil);

            mkdirs(newFile);

            if (zipEntry.isDirectory()) {
                continue;
            }

            FileOutputStream fos = new FileOutputStream(newFile);
            int len;
            while ((len = zis.read(buffer)) != -1) {
                fos.write(buffer, 0, len);
            }
            fos.close();
        }
        zis.closeEntry();
        zis.close();

        File dir = new File(tmpFile);

        for (File file : dir.listFiles()) {
            System.out.println(file.getName());

            for (File listFile : file.listFiles()) {
                System.out.println(listFile.getName());
                System.out.println(listFile.getName().substring(0,listFile.getName().lastIndexOf(".")));
            }
        }
    }

    public void mkdirs(File file) {
        if (file == null || file.exists()) {
            return;
        }
//        mkdirs(file.getParentFile());
        file.getParentFile().mkdirs();
    }

    @Test
    public void unzip() throws Exception {
        String filePath = "D:\\南宁二中标准数据模型增量\\2022-3-21南宁二中试卷答题卡\\南宁二中试卷整理（2021年1月-5月）\\584南宁二中2020-2021学年高一（下）5月月考（理科）\\南宁二中2020-2021学年高一（下）5月月考（理科）.zip";

        String tmpFile = "D:\\南宁二中标准数据模型增量\\2022-3-21南宁二中试卷答题卡\\南宁二中试卷整理（2021年1月-5月）\\584南宁二中2020-2021学年高一（下）5月月考（理科）" + "/111" + filePath.substring(filePath.lastIndexOf("\\")+1);
//        ZipFile zipFile = new ZipFile(tmpFile);

        FileOutputStream zipOutputStream = new FileOutputStream(tmpFile);

        ZipInputStream zipInputStream = new ZipInputStream(new FileInputStream(filePath), Charset.forName("gbk"));

        byte[] bytes = new byte[1024];
        int count = 0;
        while ((count = zipInputStream.read(bytes)) != -1) {
            zipOutputStream.write(bytes,0,count);
        }

        zipOutputStream.close();
        zipInputStream.close();

/*
        ZipEntry entry = null;

        while ((entry = zipInputStream.getNextEntry()) != null) {

            String paperName = entry.getName();

            long fileSize = entry.getSize();

            String fileType = paperName.substring(paperName.lastIndexOf(".")+1);

            System.out.println(paperName);
            System.out.println(fileSize);
            System.out.println(fileType);


        }*/
    }
}
